import Common from "./common/Common";
import UserInfo from "./UserInfo";
import LDataStoneManager from "./datas/LDataStoneManager";
import LDataGoldManager from "./datas/LDataGoldManager";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

export default class LevelInfo{
  
    public static lastStoneId:number = 1;
    public static levelStoneInfo:number[] = [];
    //创建石头的次数
    public static createStoneCount = 0;
    //金币掉落的概率
    public static dropGoldRateArr:number[] = []
    /*
    第一关一共掉落5块石头。击碎5个石块，包括分裂的石块。则过关					
    之后每一关增加一个石块。当到达10个石头时，每6关增加一个石块					
    增加至15个石块不在增加*/

    public static getCreateStoneCount(level:number):number{

        let stoneCount = 4;
        if(level <= 5)
        {
            stoneCount = stoneCount + level;// 1 5   2 6   3 7   4 8   5 9  6 
        }
        else
        {
            stoneCount = 10;
            stoneCount = stoneCount + Math.floor(level/6) - 1;
            if(stoneCount > 15)
            {
                stoneCount = 15;
            }
        }
        return stoneCount;
    }
    /*
    关卡初始时，刷新1个，3.5秒后再次刷新一个。最多同时出现4个。					
	当前一个刷新为4级石头时，暂停刷新。4级石头被击碎成时，刷新				
	当前一个刷新为3级石头是，下一个只能是低于他的等级				
    当前一个刷新为1或者2级石块时，下一个刷新可为任意等级石块	
    */
    private static getRandomStoneId():number{ // 0 1 2 3
        if(this.lastStoneId == 2){
          return Common.getRandom(0,2); //当前一个刷新为3级石头是，下一个只能是低于他的等级	
        }
        else{
            return Common.getRandom(0,4); //随机哪个
        }
    }
    //初始关卡信息
    public static InitLevelStoneInfo(){
        this.levelStoneInfo = [];
        Common.maxLevelScore = 0;
        this.createStoneCount = 0;

        let count = this.getCreateStoneCount(UserInfo.curLevel);
        for (let index = 0; index < count; index++) {  
            let tempStoneId = this.getRandomStoneId();
            let tempData = LDataStoneManager.GetDataById(tempStoneId)
            Common.maxLevelScore = Common.maxLevelScore+tempData.stonscore;
            this.levelStoneInfo.push(tempStoneId);
        }
    }
    //获得一个石头的id
    public static getStoneId():number{
        let stoneId = this.levelStoneInfo[this.createStoneCount];
        this.createStoneCount++;
        return stoneId;
    }
    //是否可以创建石头
    public static isCanCreateStone():boolean{
        return this.levelStoneInfo.length >= this.createStoneCount;
    }
    //是否通关
    public static isFinishLevel():boolean{
        return Common.maxLevelScore == Common.curLevelScore;
    }
    /*
    最小分数为=当前关卡数						
	正常分数=（每秒发射数量/10+火力/100）*关卡数						
	最大分数=正常分数*5						
	第一个出现的必须为最小分数						
	之后开始在下列范围内开始随机						
	分数范围	概率					
	最小分数——正常分数/5	10					
	正常分数/5——正常分数/2	45					
	正常分数/2——正常分数*2	95					
	正常分数*2——正常分数*4	98					
	正常分数*4——正常分数*5	100					
							
	当小于1时，显示为1						
	当出现小数点时， 向上取整						
							
	数字显示时，当数字超过三位数，即达到1000时，显示为1k，1480显示为1.48k						
    */
    public static getStoneScore(isFrist:boolean):number{
        if(isFrist){
            return UserInfo.curLevel; //  最小分数为=当前关卡数
        }else{
            //正常分数=（每秒发射数量/10+火力/100）*关卡数
           let normalValue = (UserInfo.bulletShootMaxCount*0.1+UserInfo.firePower*0.1)*UserInfo.curLevel; 
    
           let tempV1 = new cc.Vec2(UserInfo.curLevel,Math.ceil(normalValue/4));
           let tempV2 = new cc.Vec2(Math.ceil(normalValue/4),Math.ceil(normalValue/3));
           let tempV3 = new cc.Vec2(Math.ceil(normalValue/2),Math.ceil(normalValue));
           let tempV4 = new cc.Vec2(Math.ceil(normalValue),Math.ceil(normalValue*2));
           let tempV5 = new cc.Vec2(Math.ceil(normalValue*2),Math.ceil(normalValue*3));
           let tempV6 = new cc.Vec2(Math.ceil(normalValue*3),Math.ceil(normalValue*4));
           

           let tempv = null;

           let randomValue = Common.getRandom(0,100);

           if(randomValue > 99){
                tempv = tempV6;
            }else if(randomValue > 96){
                tempv = tempV5;
            }else if(randomValue > 66){
                tempv = tempV4;
            }else if(randomValue > 31){
                tempv = tempV3;
            }else if(randomValue > 11){
                tempv = tempV2
            }
            else{
                tempv = tempV1;
            }
            return Math.ceil(Common.getRandom(tempv.x,tempv.y));
        }
    }

    //当前关卡最大分数
    public static getCurLevelMaxScore():number{
        let normalValue = (UserInfo.bulletShootMaxCount*0.1+UserInfo.firePower+1)*UserInfo.curLevel; 
        return normalValue*5;
    }

    public static getStoneInitY():number{
        return Common.getRandom(550,1000);
    }
    /*
    金币只有石块消除后才会出现，初始概率为50，掉落【1-2】个金币								
    随着关卡增加，掉落概率提升，每过一关提升1%，提升至80%不在增加								
    金币等级每升10级，掉落的金币随机上限增加1个。增加至5个不在提升								
    如：原本为200%，随机数量为【1-3】，当提升至300%时，上限加1.随机数量变为【1-4】	*/		
    
    public static getDropGoldCount():number{

        if(Common.curLevel == 1 || Common.curLevel == 2){
            return Common.getRandom(1,3);
        }
        else if(Common.curLevel == 3){
            return Common.getRandom(1,2);
        }

        //就算金币的最大数量
        let maxDropCount = Math.floor(UserInfo.goldDropLevel*0.1) + 3;
        if(maxDropCount > 8){
            maxDropCount = 8;
        }
        // 获得一个随机数量
        let dropCount:number = Common.getRandom(1,maxDropCount);
        return dropCount
    }
    //返回掉落的id
    public static getDropGoldId():number{

        let dataLenght = LDataGoldManager.dataList.length
        let randomValue = Common.getRandom(0,100+UserInfo.goldDropLevel)
        for (let index = 0; index < dataLenght; index++) {
            let goldData = LDataGoldManager.dataList[index];
            if(randomValue > goldData.minprobability && randomValue <= goldData.maxprobability){
                return goldData.goldId;
            }   
        }
        return 0
    }
    //获得金币掉落的id概率
    public static getDropGoldRate():number[]{
        let rateArr:number[] = [];
        let totalRate:number = 0;
        for (let index = 0; index < LDataGoldManager.dataList.length; index++) { 
            let rate = 0;
            let data = LDataGoldManager.GetDataById(index);
            if(index == 0){
                rate = data.maxprobability - UserInfo.goldDropLevel;
                if(rate < 10){
                    rate = 10;
                }
            }
            else{
                rate = rate + UserInfo.goldDropLevel - totalRate;
                if(rate > data.maxprobability)
                {
                    rate = data.maxprobability;
                }
                totalRate = totalRate + rate

                if(rate > 0){
                    rate = totalRate;
                }
     
            } 
            if(rate < 0){
                rate = 0
            }

            rateArr.push(rate);
        }
        return rateArr;
    }
    /*
    消耗所需金币=（（1/（10/当前等级子弹数量）*当前子弹数量*（A）^2							
							
	A初始为1，根据等级超过20级会。之前一直以1，不变						
	当玩家等级超过30时，提升10%，即为1.1						
    之后每提升10级，都会提升10%*/
    
    //获得升级需要的金币
    public static getUpgradeNeedGold(skillLevel:number):number{
        let rate:number = 1;
        if(skillLevel > 30){
            rate = (skillLevel - 30) * 0.01 + rate
        }
        return  Math.ceil((1/(10/skillLevel))*skillLevel*rate*rate)
    }    
    /*
    // 离线金币每分钟可获得金币=（当前升级子弹所需金币*0.5+离线等级*50）/180
    */
    //获得离线金币奖励的数量
    public static getOffLineRewardGoldCount():number{

        if(UserInfo.offLineLevel <= 0){
            return 0;
        }
        var testDate = new Date(); // 1000
        let sTime = (testDate.getTime() - UserInfo.lastQuitTime)*0.001; // 秒
        let maxTime = 43200
        if(sTime > maxTime){
            sTime = maxTime;
        }
        let spendGold = LevelInfo.getUpgradeNeedGold(UserInfo.bulletShootMaxCount - 2);
        return Math.ceil((spendGold*0.5+UserInfo.offLineLevel*50)/10800*sTime)
    }
}
